package cn.ven.service;

import cn.ven.entity.MyUser;
import cn.ven.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    //根据用户名查找用户
    public MyUser findUserByAccount(String account){
        MyUser myUser = userMapper.findUserByAccount(account);
        return myUser;
    }


    //查找所有的用户
    @Override
    public List<MyUser> findAll(Integer start,Integer num) {
        List<MyUser> myUsers = userMapper.findAll(start,num);
        return myUsers;
    }

    //添加管理员
    @Override
    public void addAdm(MyUser myUser) {
        myUser.setRole("G");//管理员默认角色是G
        myUser.setClosed(1);//closed==1
        userMapper.addAdm(myUser);
    }

    //重置密码
    @Override
    public void resetPwd(MyUser myUser) {
        String password = passwordEncoder.encode("111111");//默认重置密码为111111
        myUser.setPassword(password);
        userMapper.resetPwd(myUser);
    }

    //解冻/冻结用户
    @Override
    public void closedAccount(MyUser myUser) {
        myUser.setClosed(1);//closed==1
        userMapper.closedAccount(myUser);
    }


    //查询总条数
    @Override
    public int getCount() {
        return userMapper.getCount();
    }

    //根据id查找用户
    @Override
    public MyUser findById(Integer id) {
        return userMapper.findById(id);
    }

    //注册用户
    @Override
    public void addUser(MyUser user) {
        String password =
                passwordEncoder.encode(user.getPassword());//密码加密
        user.setPassword(password);
        userMapper.addUser(user);
    }

    //修改密码
    @Override
    public void updatePwd(MyUser user) {
        user.setPassword( passwordEncoder.encode(user.getPassword()));
       userMapper.updatePwd(user);
    }

    //更新图片
    @Override
    public void updateImp(MyUser user) {
        userMapper.updateImp(user);
    }

    //更新用户信息
    @Override
    public void updateInfo(MyUser user) {
        userMapper.updateInfo(user);
    }
}
